package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dto.GridConfigDTO;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.service.base.BaseService;
import com.xdf.xzymanagementsystem.util.CustumDozerMapperUtils;
import com.xdf.xzymanagementsystem.util.Example;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.xzymanagementsystem.entity.SysUserTablePageConfig;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.xdf.xzymanagementsystem.dao.SysUserTablePageConfigMapper;
import com.xdf.xzymanagementsystem.service.SysUserTablePageConfigService;
import org.springframework.util.Assert;

/**
 * @Description : com.xdf.xzymanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : 张柯
 * @Date : 2021-07-29 15:01:03
 */
@Service
@Slf4j
public class SysUserTablePageConfigServiceImpl extends BaseService implements SysUserTablePageConfigService {
    @Autowired
    private SysUserTablePageConfigMapper dao;
    @Autowired
    private CustumDozerMapperUtils utils;

    /**
     * @Describe 新增方法
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysUserTablePageConfig record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(SysUserTablePageConfig record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<SysUserTablePageConfig> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(SysUserTablePageConfig record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(SysUserTablePageConfig record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(SysUserTablePageConfig record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<SysUserTablePageConfig> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    public SysUserTablePageConfig selectByPrimaryKey(Long id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Long id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2021-07-29 15:01:03
     */
    @Override
    public List<SysUserTablePageConfig> listQuery(SysUserTablePageConfig record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * 计算前端网格配置参数,并返回配置
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, GridConfigDTO> calculate(List<GridConfigDTO> record) throws Exception {
        List<SysUserTablePageConfig> list = utils.transSourceToTarget(record, SysUserTablePageConfig.class);
        if (CollectionUtils.isEmpty(list)) return null;
        Integer loginUserId = getLoginUserId();
        //  写入用户编号
        list.forEach(item -> item.setSysUserId(loginUserId));
        log.debug("当前是否有配置数据参数传入?{}", list != null && list.size() > 0);

        //  查询当前数据库中的配置
        List<SysUserTablePageConfig> data = dao.selectByParam(SysUserTablePageConfig.builder()
                .sysUserId(loginUserId)
                .sysMenuId(list.stream().findFirst().map(SysUserTablePageConfig::getSysMenuId)
                        .orElseThrow(() -> new RuntimeException("无法获取到菜单编号！操作失败")))
                .build());
        Set<String> s = data.stream().map(SysUserTablePageConfig::getGridId).collect(Collectors.toSet());
        List<SysUserTablePageConfig> insertData = list.stream().filter(x -> !s.contains(x.getGridId())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(insertData)) {
            dao.multiInsert(insertData);
        } else {
            list = data;
        }

        //  转换后输出
        return list.stream()
                .collect(Collectors.toMap(SysUserTablePageConfig::getGridId, x -> utils.transObject(x, GridConfigDTO.class), (x, y) -> x));
    }

    /**
     * 根据条件修改
     *
     * @param record
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByParam(GridConfigDTO record) throws Exception {
        SysUserTablePageConfig param = utils.transObject(record, SysUserTablePageConfig.class);
        Assert.state(param != null && StringUtils.isNotEmpty(param.getGridId()) && param.getSysMenuId() != null,
                ResponseEnum.SAVE_DATA_ERROR.getName());

        param.setSysUserId(getLoginUserId());
        List<SysUserTablePageConfig> list = dao.selectByParam(SysUserTablePageConfig.builder()
                .gridId(record.getGridId())
                .sysMenuId(record.getSysMenuId())
                .sysUserId(param.getSysUserId())
                .build());

        //  根据后台的数据选择性更新
        if (CollectionUtils.isEmpty(list)) {
            return dao.insertSelective(param);
        } else {
            if (list.size() > 1) {
                Long[] deleteKey = list.stream().skip(1).map(SysUserTablePageConfig::getId).toArray(Long[]::new);
                dao.deleteByExample(new Example().andIn("id", deleteKey));
            }
            //  修改
            param.setId(list.get(0).getId());
            return dao.updateByPrimaryKeySelective(param);
        }
    }
}